﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _3_LengthOfLongestSubstring
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "ojyseenuxxpohrysqixldpki";

            int result = LengthOfLongestSubstring(str);


            Console.WriteLine(result);

            Console.ReadLine();
        }

        private static int LengthOfLongestSubstring(string s)
        {
            if (s.Length < 2)
                return s.Length;

            int result = 1;

            Dictionary<char, int> dicChar = new Dictionary<char, int>();
            Dictionary<char, string> dicValue = new Dictionary<char, string>();

            foreach (char c in s)
            {
                if (!dicChar.ContainsKey(c) && !dicValue.ContainsKey(c))
                {
                    dicChar.Add(c, 0);
                    //dicValue.Add(c, c.ToString());
                    dicValue.Add(c, string.Empty);
                }


                foreach (KeyValuePair<char, int> itemChar in dicChar)
                {
                    if (!string.IsNullOrEmpty(dicValue[itemChar.Key]) && dicValue[itemChar.Key].IndexOf(c) == -1)
                    {
                        dicValue[itemChar.Key] += c;
                    }
                    else
                    {
                        dicValue[itemChar.Key] = string.Empty;
                        if (itemChar.Key == c)
                        {
                            dicValue[itemChar.Key] = c.ToString();
                        }
                    }

                    if (result < dicValue[itemChar.Key].Length)
                    {
                        result = dicValue[itemChar.Key].Length;
                    }
                }
            }

            return result;
        }
    }
}
